const Router = require('koa-router');
const router = new Router();
const {db, Op, QueryTypes} = require("../../models/db");
const tools = require("../../tools/tool");

// 页面通用参数加载
router.post('/common/getPageParams', async ctx => {
    let page = ctx.request.body.page;
    let params = await db.query(` select * from form_cust_v5 where page= '${page}' `, {type: QueryTypes.SELECT, plain: true})
    let fieldsArr = [
        {
            field: 'id',
            title: 'ID',
            isKey: true,
            visible: false,
            add: false,
            edit: false
        },
        {
            field: 'seq',
            type: 'seq',
            title: '#',
            width: 60,
            align: 'center',
            visible: true,
            fixed: 'left'
        },
        {
            field: 'checked',
            type: 'checkbox',
            title: '',
            width: 40,
            fixed: 'left',
            visible: true,
            align: 'center'
        },
    ]

    // let ctimeField = "{field: 'ctime', title: '创建时间',excelOut:true,sortable:true, width: 105 }";
    // let utimeField = "{field: 'utime', title: '更新时间',excelOut:true,sortable:true,  width: 105 }";
    // let dtimeField = "{field: 'dtime', title: '删除时间',excelOut:true,sortable:true,   width: 105 }";
    // let fileName = params.name; // 导出的文件名称
    let cols = JSON.parse(params.fields); // 查询。编辑的字段
    cols.map(item => fieldsArr.push(item));
    let toolbarstr = params.toolbar; // toolbar
    // let contextmenustr = params.contextmenu; // contextmenu
    let btns = [
        // {
        //     code: 'refresh',
        //     name: '刷新',
        //     prefixIcon: 'fa fa-refresh',
        //     title: '重置所有条件并刷新当前页',
        //     disabled: false
        // },
        {
            code: 'search',
            name: '查询',
            prefixIcon: 'fa fa-search',
            title: '高级搜索',
            disabled: false
        },
        {
            code: 'auth',
            name: '权限',
            prefixIcon: 'fa fa-user-o',
            title: '编辑权限',
            disabled: false
        },
        {
            code: 'setPwd',
            name: '密码',
            prefixIcon: 'fa fa-user-o',
            title: '设置密码',
            disabled: false
        },
        {
            code: 'add',
            name: '新增',
            prefixIcon: 'fa fa-plus',
            title: '新增 1 条数据',
            disabled: false
        },
        {
            code: 'edit',
            name: '编辑',
            prefixIcon: 'fa fa-edit',
            title: '编辑 1 条数据',
            disabled: false
        },
        {
            code: 'accounted',
            name: '审核',
            prefixIcon: 'fa fa-rmb',
            title: '审核',
            disabled: false
        },
        {
            code: 'del',
            name: '删除',
            prefixIcon: 'fa fa-trash',
            title: '删除选中条目',
            disabled: false
        },
        {
            code: 'copy',
            name: '复制',
            prefixIcon: 'fa fa-copy',
            title: '复制单元格内容',
            disabled: false
        },
        {
            code: 'import',
            name: '导入',
            prefixIcon: 'fa fa-upload',
            title: '导入数据',
            disabled: false
        },
        {
            code: 'export',
            name: '导出',
            prefixIcon: 'fa fa-download',
            title: '导出数据',
            disabled: false
        },
        {
            code: 'print',
            name: '打印',
            prefixIcon: 'fa fa-print',
            title: '打印页面内容',
            disabled: false
        },
        {
            code: 'columns',
            name: '选列',
            prefixIcon: 'fa fa-columns.js',
            title: '选择要显示的列',
            disabled: false
        },
        {
            code: 'close',
            name: '关闭',
            prefixIcon: 'fa fa-close',
            title: '关闭',
            disabled: false
        },
        {
            code: 'lock',
            name: '锁屏',
            prefixIcon: 'fa fa-lock',
            title: '离开并锁定屏幕',
            disabled: false
        },
    ]
    let btnList = []

    for (let btn in btns) {
        if (toolbarstr.indexOf(btn.code) !== -1) {
            btnList.add(btn)
        }
    }
    let contextmenu = []
    for (let btn in btns) {
        // if (contextmenustr.indexOf(btn.code) != -1) {
        //     contextmenu.add(btn)
        // }
    }
    fieldsArr.map(it => {
        if (it.field === 'beginDate') {
            it.default = tools.getDay('', -15)
        }
        if (it.field === 'endDate') {
            it.default = tools.getDay()
        }
    })

    return ctx.suc('', {
        btnList,
        contextmenu,
        columns: fieldsArr,
        params: params
    })
})








module.exports = router;